﻿using Convertor.Interface;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.RemoveDuplicate
{
    public class Plugin : PluginBase, IEnumerable
    {

        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.RemoveDuplicate;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.RemoveDuplicate;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Array") + " - " + UILanguage.FindResource("Plugin_RemoveDuplicate_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_RemoveDuplicate_Desc"); }
        }


        

        

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            List<string> listString = new List<string>();
            var values = content.OutputContent as IList;
            if (values == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_NotArray"), LogTypes.Alert); }
                return content;
            }

            foreach (string v in values)
            {
                if (!string.IsNullOrEmpty(v))
                {
                    if (!listString.Contains(v))
                        listString.Add(v);
                }
            }
            //List<object> listString = ((List<object>)content.Output).Distinct().ToList();
            if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_RemoveDuplicate_ConvertSuccess"), values.Count - listString.Count), LogTypes.Normal); }

            return new ConvertParamters(content.LastInputContent, listString);

        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
